
import time
import random

from dexen.user.libs import constants as dc, server_api

from libs import input_reading, constants

""" GLOBALS """
pop_api = server_api.PopAPIFactory()
boxes = input_reading.read_input()
n_boxes = len(boxes)

def generate_individual():
    
    individual = {
        constants.GENOTYPE : {
            "gamma_x" : range(n_boxes),
            "gamma_y" : range(n_boxes),
            "orient" : [None] * n_boxes,
            "encoding" : [None] * n_boxes
        }
    }
    genotype = individual[constants.GENOTYPE]
        
    random.shuffle(genotype['gamma_x'])
    random.shuffle(genotype['gamma_y'])
    
    for i, box_no in enumerate(genotype['gamma_x']):
        genotype['encoding'][box_no] = dict(x=i)
    
    for i, box_no in enumerate(genotype['gamma_y']):
        genotype['encoding'][box_no]['y'] = i
        
    for i in range(n_boxes):
        genotype['orient'][i] = random.randint(0, 1)
        
    return individual  

def initialise():
    print "I am in initialization"
    
    individuals = []
    
    #initial_size = pop_api.getGlobalData()[constants.INITIALIZATION]
    
    initial_size = 400
    
    while initial_size > 0:
        individual = generate_individual()
        #print individual[constants.GENOTYPE]
        individuals.append(individual)
        initial_size -= 1
        
    #pop_api.uploadIndividuals(individuals)
    return individuals
